//
//  FunnyPhotoViewController.m
//  USB_Reader
//
//  Created by xiaozao on 15/3/24.
//  Copyright (c) 2015年 Tony_Wang. All rights reserved.
//

#import "FunnyPhotoViewController.h"
#import "FunPhotoTableViewCell.h"
#import "AFNetworking.h"
#import "FunPhotoDataJSON.h"
#import "FunDataHTTP.h"
#import "MBProgressHUD.h"
@interface FunnyPhotoViewController () <UITableViewDataSource, UITableViewDelegate,MBProgressHUDDelegate>
@property (nonatomic, retain) UITableView *funPhotoTableView;
@property (nonatomic, retain) NSMutableArray *arr;
@property (nonatomic, assign) NSInteger rowsNumber;
@property (nonatomic, retain) MBProgressHUD *progress;
@end

@implementation FunnyPhotoViewController

- (void)hudWasHidden:(MBProgressHUD *)hud
{
    
    [_progress removeFromSuperview];
    [_progress release];
    _progress  = nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.funPhotoTableView = [[UITableView alloc] initWithFrame:CGRectMake(10, 0, self.view.frame.size.width - 20, self.view.frame.size.height) style:UITableViewStylePlain];

    self.funPhotoTableView.dataSource = self;
    self.funPhotoTableView.delegate = self;
    [self.view addSubview:_funPhotoTableView];
    
    self.rowsNumber = 10;
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    
    self.progress = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [_progress show:YES];
    
    [self dataAsk];
    
    [_funPhotoTableView release];

}

- (void)dataAsk
{
    NSTimeInterval time = [[NSDate date] timeIntervalSince1970];
    CGFloat timeStr = time *10000;
    NSString *times = [NSString stringWithFormat:@"%f",timeStr];
    NSString *timeTip = [times substringToIndex:14];
    NSString *urlStr = [NSString stringWithFormat:@"http://m2.qiushibaike.com/article/list/imgrank?count=30&page=1&AdID=%@",timeTip];
    [FunDataHTTP requsetWithURL:urlStr  cache:@"cachePhoto"  completion:^(NSData *resultData) {

        NSMutableArray *arr = [NSMutableArray array];
            id result = [NSJSONSerialization JSONObjectWithData:resultData options:NSJSONReadingMutableContainers error:nil];
            arr = [result objectForKey:@"items"];
        self.rowsNumber = arr.count;
        
        self.arr = [NSMutableArray array];
        
        for (NSMutableDictionary *dic in arr) {
            FunPhotoDataJSON *data = [[FunPhotoDataJSON alloc] initWithDictionary:dic];
            [self.arr addObject:data];
            [data release];
        }
        [_progress hide:YES];
        [self.funPhotoTableView reloadData];
    }];
}




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.rowsNumber;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"cell";
    FunPhotoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (cell == nil) {
        cell = [[[FunPhotoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str] autorelease];
    }
    // 传值
    cell.data = [self.arr objectAtIndex:indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 高度
    FunPhotoDataJSON *heighCount = [self.arr objectAtIndex:indexPath.row];
    NSString *str = heighCount.content;
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:20]};
    CGRect rect = [str boundingRectWithSize:CGSizeMake(self.funPhotoTableView.frame.size.width, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    NSMutableDictionary *funDic = heighCount.image_size;
    NSMutableArray *array = [funDic objectForKey:@"m"];
    CGFloat width = [[array objectAtIndex:0] floatValue];
    CGFloat heigh = [[array objectAtIndex:1] floatValue];
    if (width == 0 && heigh == 0) {
        width = 1;
        heigh = 1;
        return 100;
    } else {
    
    CGFloat factWidthWithWidth = self.funPhotoTableView.frame.size.width / width;
    CGFloat factHeigh = heigh * factWidthWithWidth;
    
        NSLog(@"总计高度 : %lf",rect.size.height + factHeigh);
        return (rect.size.height + factHeigh + 30);
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
